/*
 * @Author: haungyan
 * @Date: 2020-11-23 20:34:54
 * @LastEditors: haungyan
 * @LastEditTime: 2020-12-29 11:41:47
 * @Description: file content
 * @FilePath: \fed-e-task-03-01\code\mini-vue\js\compiler.js
 */
class Compiler {
    constructor(vm) {
        this.el = vm.$el
        this.vm = vm
        this.compile(this.el)
    }

    // 编译模板，处理文本节点和元素节点
    compile(el) {
        const childNodes = el.childNodes
        Array.from(childNodes).forEach(node => {
            if (this.isTextNode(node)) {
                this.compileText(node)
            } else if (this.isElementNode(node)) {
                this.compileElement(node)
            }

            // 判断是否还有字节点
            if (node.childNodes && node.childNodes.length) {
                this.compile(node)
            }
        })
    }

    // 编译元素节点，处理指令
    compileElement(node) {
        // console.log(node)
        // 遍历节点
        Array.from(node.attributes).forEach(attr => {
            // console.dir(attr)
            const { name, value } = attr // 获取自定义属性的属性和值
            if (this.isDirective(name)) {
                let attrName = name.substr(2)
                this.update(node, attrName, value)
            }
        })
    }
    /**
     * 
     * @param {Element} node 
     * @param {string} key 
     * @param {string} attrName 
     */
    update(node, attrName, key) {
        let updaterKey = `${attrName}Updater`
        let updateFn = this[updaterKey]
        let eventType = null

        if (attrName.startsWith('on:')) {
            eventType = attrName.replace('on:', '')
            this.onUpdater.call(this, node, this.vm[key], key, eventType)
        } else {
            updateFn && updateFn.call(this, node, this.vm[key], key)
        }
        
        // this.textUpdater()
    }
    // 处理v-text指令
    textUpdater(node, value, key) {
        // console.log(value)
        node.textContent = value
        new Watcher(this.vm, key, newValue => {
            node.textContent = newValue
        })
    }

    modelUpdater(node, value, key) {
        node.value = value
        new Watcher(this.vm, key, newValue => {
            node.value = newValue
        })
        // 数据双向绑定
        node.addEventListener('input', () => {
            this.vm[key] = node.value
        })
    }

    // 处理事件绑定，无法处理传参的情况
    onUpdater(node, value, key, eventType) {
        // console.log(eventType, key)

        node.addEventListener(eventType, value)
        new Watcher(this.vm, key, newValue => {
            node.removeEventListener(eventType, value)
            node.addEventListener(eventType, newValue)
        })
    }

    htmlUpdater(node, value, key) {
        node.innerHTML = value
        new Watcher(this.vm, key, newValue => {
            node.innerHTML = newValue
        })
    }

    // 编译文本节点，处理插值表达式
    compileText(node) {
        let reg = /\{\{(.+?)\}\}/
        let value = node.textContent
        if (reg.test(value)) {
            const key = RegExp.$1.trim()
            node.textContent = value.replace(reg, this.vm[key])

            // 创建观察者
            new Watcher(this.vm, key, (newValue) => {
                node.textContent = newValue
            })
        }
    }

    // 判断元素是否是指令
    isDirective(attrName) {
        return attrName.startsWith('v-')
    }

    // 判断节点是否是文本节点
    isTextNode(node) {
        return node.nodeType === 3
    }

    // 判断节点是否是元素节点
    isElementNode(node) {
        return node.nodeType === 1
    }
}